package com.situ.firmadmin.controller;


import com.situ.firmadmin.pojo.Auth;
import com.situ.firmadmin.pojo.User;
import com.situ.firmadmin.pojo.query.AuthQuery;
import com.situ.firmadmin.service.IAuthService;
import com.situ.firmadmin.service.IUserService;
import com.situ.firmadmin.util.PageResult;
import com.situ.firmadmin.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/auth")
public class AuthController {
    @Autowired
    private IAuthService authService;
    @Autowired
    private IUserService userService;

    @GetMapping("/selectByRole")
    public Result selectByRole(){
        User user=userService.getUser();
        Integer role =user.getRole();
        List<Auth> authList = authService.selectByRole(role);
        return Result.ok(authList);
    }

    @GetMapping("/list")
    public Result selectByPage(AuthQuery authQuery){
        PageResult pageResult=authService.selectByPage(authQuery);
        Map<String,Object> map=new HashMap<>();
        map.put("total",pageResult.getCount());
        map.put("list",pageResult.getData());
        return Result.ok(map);
    }

    @PostMapping("/add")
    public Result insert(@RequestBody Auth auth){
        authService.insert(auth);
        return Result.ok("添加成功！");
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        Auth auth=authService.selectById(id);
        return Result.ok(auth);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Auth auth){
        authService.update(auth);
        return Result.ok("修改成功！");
    }

    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id){
        authService.deleteById(id);
        return Result.ok("删除成功！");
    }

    @DeleteMapping("/deleteAll/{ids}")
    public Result deleteAll(@PathVariable Integer[] ids){
        authService.deleteAll(ids);
        return Result.ok("删除成功！");
    }
    
    @PutMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Integer id, @PathVariable Integer status) {
        Auth auth=new Auth();
        auth.setId(id);
        auth.setStatus(status);
        authService.updateStatus(auth);
        return Result.ok("修改成功！");
    }
    
    //动态菜单栏
    @GetMapping("/selectByRole2")
    public Result selectByRole2(){
        User user=userService.getUser();
        List<Auth> authList = authService.selectByRole2(user.getRole());
        System.out.println(authList);
        return Result.ok(authList);
    }
    
    @GetMapping("/selectAllParent")
    public Result selectAllParent(){
        List<Auth> parentList=authService.selectAllParent();
        return Result.ok(parentList);
    }
}
